package com.dapeis.api.controller.collocation.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.swing.text.StyleConstants;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ag.core.exception.InvalidParamException;
import com.ag.core.exception.SysTipsException;
import com.ag.core.utils.AgAssert;
import com.ag.utils.NumberUtils;
import com.ag.utils.json.AGsonBuilder;
import com.dapeis.api.controller.RestController;
import com.dapeis.api.controller.collocation.IShopController;
import com.dapeis.core.utils.StringUtil;
import com.dapeis.core.utils.StylistConstants;
import com.dapeis.domain.Shop;
import com.dapeis.service.ShopService;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

/**
 * 搭配师服务
 * 
 * @author yuanyong
 *
 */
@Controller
@RequestMapping("/dps/shop")
public class ShopController extends RestController implements IShopController {

    @Resource
    ShopService shopService;

    @Override
    public JsonElement add(@RequestParam(required = false) Integer id,
	    @RequestParam Integer type, @RequestParam String price,
	    @RequestParam String content) {
	checkParma(type, price, content);
	Float priceFooat = null;
	try {
	    Float moneyFloat = new Float(price);
	    priceFooat = new Float(moneyFloat * 100);
	} catch (Exception e) {
	    throw new InvalidParamException("定价参数非法!");
	}

	Shop shop = new Shop();
	shop.buildShop(type, getThreadLocalCollocationId(),
		priceFooat.intValue(), content);
	if (NumberUtils.isValid(id))
	    shop.setId(id);
	shopService.addOrUpdate(shop);
	JsonObject json = buildSuccess();
	return json;
    }

    @Override
    public JsonElement getSingleShop(
	    @RequestParam(required = false) Integer id,
	    @RequestParam(required = false) Integer type) {
	Shop shop = shopService.getSingleShop(id, type,
		getThreadLocalCollocationId());
	JsonObject json = buildSuccess();
	json.add("data", AGsonBuilder.create().toJsonTree(shop));
	return json;
    }

    @Override
    public JsonElement getMyShop() {
	List<Shop> shops = shopService
		.getShopByCollocationId(getThreadLocalCollocationId());
	JsonObject json = buildSuccess();
	json.add("data", AGsonBuilder.create().toJsonTree(shops));
	return json;
    }

    /**
     * 检查参数
     * 
     * @param type
     * @param price
     * @param content
     * @return
     */
    protected boolean checkParma(Integer type, String price, String content) {
	AgAssert.isTrue(StylistConstants.checkShopTypeValue(type) == true,
		InvalidParamException.class, "服务类型参数非法！");
	AgAssert.notNull(StringUtil.isBlank(price),
		InvalidParamException.class, "价格参数非法!");
	AgAssert.notNull(StringUtil.isBlank(content),
		InvalidParamException.class, "内容参数非法!");
	return false;
    }

}
